package com.sywl.glzh.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class GlobalInterceptor extends HandlerInterceptorAdapter {
	
	@Override
	public boolean preHandle(HttpServletRequest request,
			HttpServletResponse response, Object handler) throws Exception {
		/*
		 * 如果访问的地址为： http://127.0.0.1/contact
		 * 判断其uri是否包含/en，如果不包含则截取contact
		 * 如果访问的地址为 ： http://127.0.0.1/en/contact
		 * 还是截取到 contact
		 * 包括参数
		 */
		String uri = request.getRequestURI();
		if(uri.indexOf("/en") < 0) {
			uri = uri.substring(1);
		} else {
			uri = uri.substring(4);
		}
		StringBuilder params = new StringBuilder();
		int i = 0;
		for (Object o : request.getParameterMap().keySet()) {
			String paramName = (String)o;
			if (i++ > 0) params.append("&");
			params.append(paramName + "=" + request.getParameter(paramName));
		}
		if (params.length() > 0) {
			request.setAttribute("uri", uri + "?" + params);
		} else {
			request.setAttribute("uri", uri);
		}
		
		return super.preHandle(request, response, handler);
	}
}
